{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.6.12 64-bit ('Begin': conda)",
   "metadata": {
    "interpreter": {
     "hash": "5418aa4018a4a6d4e7a95d1864632b051ddd71a0141746edebf6fac21ca2c6f9"
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "# C11. 文件"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "## 11.1 文件打开\n",
    "\n",
    "文件模式：\n",
    "-   r：读取模式(默认值)\n",
    "-   w：写入模式\n",
    "-   x：独占写入模式\n",
    "-   a：附加模式\n",
    "-   b：二进制模式(与其他模式结合使用)\n",
    "-   t：文本模式(默认值，与其他模式结合使用)\n",
    "-   +：读写模式(与其他模式结合使用)"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "## 11.2 基本方法"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "### 11.2.1 读取和写入"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "f=open('somefile.txt','w')\n",
    "f.write('Hello, ')\n",
    "f.write('World!')\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Hell\no, World!\n"
     ]
    }
   ],
   "source": [
    "f=open('somefile.txt','r')\n",
    "print(f.read(4))\n",
    "print(f.read())"
   ]
  },
  {
   "source": [
    "### 文件的行\n",
    "\n"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "### 文件关闭\n",
    "\n"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "### 文件的基本方法\n",
    "\n"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "## 11.3 文件内容迭代"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "### 11.3.1 每次一个字节"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Hello, World!"
     ]
    }
   ],
   "source": [
    "def process(char):\n",
    "    print(char,end='')\n",
    "    \n",
    "with open('somefile.txt') as f:\n",
    "    char=f.read(1)\n",
    "    while char:\n",
    "        process(char)\n",
    "        char=f.read(1)"
   ]
  },
  {
   "source": [
    "### 11.3.2 每次一行"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Hello, World!"
     ]
    }
   ],
   "source": [
    "def process(char):\n",
    "    print(char,end='')\n",
    "    \n",
    "with open('somefile.txt') as f:\n",
    "    char=f.readline()\n",
    "    while char:\n",
    "        process(char)\n",
    "        char=f.read(1)"
   ]
  },
  {
   "source": [
    "### 11.3.3 读取所有内容"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Hello, World!\nHello, World!"
     ]
    }
   ],
   "source": [
    "def process(char):\n",
    "    print(char,end='')\n",
    "    \n",
    "with open('somefile.txt') as f:\n",
    "    for char in f.read():\n",
    "        process(char)\n",
    "\n",
    "print()\n",
    "\n",
    "with open('somefile.txt') as f:\n",
    "    for line in f.readline():\n",
    "        process(line)\n"
   ]
  },
  {
   "source": [
    "### 11.3.4 使用 fileinput 实现延迟行迭代\n",
    "\n"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "### 11.3.5 文件迭代器"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用 上下文管理器，确保文件得以关闭\n",
    "with open(filename) as f:\n",
    "    for line in f:\n",
    "        process(line)\n",
    "\n",
    "# 使用 Python 负责文件关闭\n",
    "for line in open(filename):\n",
    "    process(line)"
   ]
  },
  {
   "source": [
    "## 11.4 小结\n",
    "\n",
    "-   类似于文件的对象：能够支持 read 和 readline 等方法的对象，类似于文件的对象\n",
    "-   打开和关闭文件：使用函数 open 打开文件，确保即使发生错误时文件也将被关闭，可以使用 with 语句\n",
    "-   模式和文件类型：打开文件时，还可指定模式\n",
    "-   标准流：三个标准流都是类似于文件的对象\n",
    "-   读取和写入：要从文件或者类似于文件的对象中读取和宣州\n",
    "-   读取和写入行：要从文件中读取或者写入行\n",
    "-   迭代文件内容：迭代文本文件中的行"
   ],
   "cell_type": "markdown",
   "metadata": {}
  }
 ]
}